package com.crr.crud;

import com.crr.bean.PageBean;
import com.crr.bean.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;


import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;


public abstract class MyBaseServiceImpl<T,M extends MyBaseMapper<T>> implements MyBaseService<T> {

    @Autowired(required = false)
    protected M baseMapper;


    @Override
    public int saveEntity(T entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int deleteEntity(Serializable id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateEntity(T entity) {
        return baseMapper.updateById(entity);
    }

    @Override
    public T getEntity(Serializable id) {
        return (T) baseMapper.selectById(id);
    }
    @Override
    public <P extends PageBean> PageResult<T> query(P condition) {
        PageHelper.startPage(condition.getPage(),condition.getPagerSize());
        List list = baseMapper.query(condition);
        PageInfo info = new PageInfo(list);
        return new PageResult<T>(condition.getPage(), condition.getPagerSize(),info.getTotal(),list);
    }

}
